package kt.hkit.androidcomplete01.chapter08;

import java.util.ArrayList;

import kr.hkit.androidcomplete01.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;

public class FreeLineEx extends Activity {
	ArrayList<Vertex> arVertex;
	MyView vw;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		arVertex = new ArrayList<Vertex>();
		vw = new MyView(this);
		setContentView(vw);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.freeline_menu, menu);
		return true;
	}

	public boolean onPrepareOptionsMenu(Menu menu) {
		int color = vw.paint.getColor();

		if (color == Color.RED) {
			menu.findItem(R.id.ch08_freeline_red).setChecked(true);
		}
		if (color == Color.BLUE) {
			menu.findItem(R.id.ch08_freeline_blue).setChecked(true);
		}
		if (color == Color.WHITE) {
			menu.findItem(R.id.ch08_freeline_white).setChecked(true);
		}
		if (color == Color.GREEN) {
			menu.findItem(R.id.ch08_freeline_green).setChecked(true);
		}
		if (color == Color.YELLOW) {
			menu.findItem(R.id.ch08_freeline_yellow).setChecked(true);
		}

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.ch08_freeline_clear:
			arVertex.removeAll(arVertex);
			break;
		case R.id.ch08_freeline_clear2:
			vw.paint.setColor(Color.BLACK);
			vw.paint.setStrokeWidth(15);
			break;
		case R.id.ch08_freeline_red:
			vw.paint.setColor(Color.RED);
			break;
		case R.id.ch08_freeline_blue:
			vw.paint.setColor(Color.BLUE);
			break;
		case R.id.ch08_freeline_white:
			vw.paint.setColor(Color.WHITE);
			break;
		case R.id.ch08_freeline_green:
			vw.paint.setColor(Color.GREEN);
			break;
		case R.id.ch08_freeline_yellow:
			vw.paint.setColor(Color.YELLOW);
			break;

		}
		return true;
	}

	class MyView extends View {
		Paint paint;

		public MyView(Context context) {
			super(context);
			paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(Color.RED);
			paint.setStrokeWidth(3);
		}

		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			canvas.drawColor(Color.BLACK);
			for (int i = 0; i < arVertex.size(); i++) {
				paint.setColor(arVertex.get(i).mColor);
				paint.setStrokeWidth(arVertex.get(i).mSize);
				if (arVertex.get(i).draw) {
					canvas.drawLine(arVertex.get(i - 1).x,
							arVertex.get(i - 1).y, arVertex.get(i).x,
							arVertex.get(i).y, paint);
				}
			}
		}

		public boolean onTouchEvent(MotionEvent event) {
			super.onTouchEvent(event);
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				arVertex.add(new Vertex(event.getX(), event.getY(), false));
				return true;
			}
			if (event.getAction() == MotionEvent.ACTION_MOVE) {
				arVertex.add(new Vertex(event.getX(), event.getY(), true, 
						paint.getColor(), paint.getStrokeWidth()));
				invalidate();
				return true;
			}
			return false;
		}

	}
}
